
import dayjs from 'dayjs';
export function setData(dateArr) {
	const getNewDate = (time) => {
		let date = new Date(time).getTime() + 1000 * 60 * 60 * 24;
		let Y = new Date(date).getFullYear() + '-';
		let M = (new Date(date).getMonth() + 1 < 10 ? '0' + (new Date(date).getMonth() + 1) : new Date(date)
			.getMonth() + 1) + '-';
		let D = new Date(date).getDate() < 10 ? '0' + new Date(date).getDate() : new Date(date).getDate();
		return Y + M + D;
	};

	//　新日期数组
	let newDateArr = [];
	newDateArr.push(dateArr[0], dateArr[dateArr.length - 1]);
	let startDate = newDateArr[newDateArr.length - 2].date;
	let endDate = newDateArr[newDateArr.length - 1].date;
	let startStamp = new Date(startDate).getTime();
	let endStamp = new Date(endDate).getTime();
	let diffCounts = Math.abs(startStamp - endStamp) / 1000 / 60 / 60 / 24;
	// 得出倒数第二天和倒数第一天的相差天数，依次递减，直到相差１天，表示是相邻的两天
	for (let index = diffCounts; index > 1; index--) {
		//　根据时间戳得出应该往后加的日期
		getNewDate(newDateArr[newDateArr.length - 2].date);
		//　把新的日期从倒数第二个位置追加到新日期数组
		newDateArr.splice(newDateArr.length - 1, 0, {
			date: getNewDate(newDateArr[newDateArr.length - 2].date),
			count: 0,
		});
	}
	newDateArr.forEach((newItem) => {
		dateArr.forEach((oldItem) => {
			if (newItem.date === oldItem.date) {
				newItem.count = oldItem.count;
			}
		});
	});
	// 　　　 console.log('原来的缺失日期',dateArr);
	//       console.log('补全后的日期',newDateArr);
	return newDateArr

}
export function friendlyDate(timestamp) {
    const formats = {
      year: "%n% 年前",
      month: "%n% 月前",
      day: "%n% 天前",
      hour: "%n% 小时前",
      minute: "%n% 分钟前",
      second: "%n% 秒前"
    };
    let seconds = dayjs(new Date()).diff(timestamp, "second");
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    const days = Math.floor(hours / 24);
    const months = Math.floor(days / 30);
    const years = Math.floor(months / 12);
  
    let diffType = "";
    let diffValue = 0;
    if (years > 0) {
      diffType = "year";
      diffValue = years;
    } else {
      if (months > 0) {
        diffType = "month";
        diffValue = months;
      } else {
        if (days > 0) {
          diffType = "day";
          diffValue = days;
        } else {
          if (hours > 0) {
            diffType = "hour";
            diffValue = hours;
          } else {
            if (minutes > 0) {
              diffType = "minute";
              diffValue = minutes;
            } else {
              diffType = "second";
              diffValue = seconds === 0 ? (seconds = 1) : seconds;
            }
          }
        }
      }
    }
    return formats[diffType].replace("%n%", diffValue);
  }
  
  var chnNumChar =  ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  var chnUnitSection = ["", "万", "亿", "万亿", "亿亿"];
  var chnUnitChar = ["", "十", "百", "千"];

/**
 * 获取中文
 * @param num
 * @returns {*}
 */
 function SectionToChinese(section) {
 var ori = section;
 var strIns = '';
 var chnStr = '';
 var unitPos = 0;
 var zero = true;
 while (section > 0) {
 var v = section % 10;
 if (v === 0) {
 if (!zero) {
 zero = true;
 chnStr = chnNumChar[v] + chnStr;
 }
 } else {
 zero = false;
 strIns = chnNumChar[v];
 strIns += chnUnitChar[unitPos];
 chnStr = strIns + chnStr;
 }
 unitPos++;
 section = Math.floor(section / 10);
 }
 if (ori < 20) {
 chnStr = chnStr.replace('一十', '十');
 }
 return chnStr;
}

/**
 * 数字转中文
 * @param num
 * @returns {*}
 */
 export function numberToChanie(num) {
 var unitPos = 0;
 var strIns = '', chnStr = '';
 var needZero = false;

 if (num === 0) {
 return JsUlits.chnNumChar[0];
 }

 while (num > 0) {
 var section = num % 10000;
 if (needZero) {
 chnStr = chnNumChar[0] + chnStr;
 }
 strIns = SectionToChinese(section);
 strIns += (section !== 0) ? chnUnitSection[unitPos] : chnUnitSection[0];
 chnStr = strIns + chnStr;
 needZero = (section < 1000) && (section > 0);
 num = Math.floor(num / 10000);
 unitPos++;
 }
 return chnStr;
}

// 0超级超级管理员\1管理员\2监理\3经理\4门店客户
const authority=[
	{
		authorityCode: 100,
		authorityId: 1,
		authorityName: "邀请用户"
	},{
		authorityCode: 100,
		authorityId: 1,
		authorityName: "查看验收评分"
	}
]

export function isPermission(authorityId){
	const authority = JSON.parse(uni.getStorageSync('authority'))||null
	if(authority){
		return authority.some(item=>item.authorityId==authorityId)
	}
	return false
} 

